package com.yjx.gulimall.product.service.impl;

import com.yjx.gulimall.product.dao.BrandDao;
import com.yjx.gulimall.product.dao.CategoryDao;
import com.yjx.gulimall.product.entity.BrandEntity;
import com.yjx.gulimall.product.entity.CategoryEntity;
import com.yjx.gulimall.common.vo.product.BrandVO;
import com.yjx.gulimall.common.vo.product.CategoryBrandVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjx.gulimall.common.utils.PageUtils;
import com.yjx.gulimall.common.utils.Query;

import com.yjx.gulimall.product.dao.CategoryBrandRelationDao;
import com.yjx.gulimall.product.entity.CategoryBrandRelationEntity;
import com.yjx.gulimall.product.service.CategoryBrandRelationService;


@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryDao categoryDao;

    @Autowired
    private BrandDao brandDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        Long brandId = categoryBrandRelation.getBrandId();
        Long catelogId = categoryBrandRelation.getCatelogId();

        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = baseMapper.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId).eq("catelog_id", catelogId));

        if(categoryBrandRelationEntities.isEmpty()){
            BrandEntity brandEntity = brandDao.selectById(brandId);
            CategoryEntity categoryEntity = categoryDao.selectById(catelogId);

            categoryBrandRelation.setBrandName(brandEntity.getName());
            categoryBrandRelation.setCatelogName(categoryEntity.getName());

            this.save(categoryBrandRelation);
        }
    }

    @Override
    public List<CategoryBrandVO> getCategoryBrandList(Long brandId) {

        List<CategoryBrandRelationEntity> categoryBrandRelationEntities = this.list(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));

        List<CategoryBrandVO> collect = categoryBrandRelationEntities.stream().map(obj -> {
            CategoryBrandVO categoryBrandVO = new CategoryBrandVO();
            categoryBrandVO.setBrandName(obj.getBrandName());
            categoryBrandVO.setCatelogName(obj.getCatelogName());
            categoryBrandVO.setId(obj.getId());
            return categoryBrandVO;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public List<BrandVO> getBrandListByCatId(Long catId) {

        List<CategoryBrandRelationEntity> categoryBrandRelationEntityList = baseMapper.selectList(
                new QueryWrapper<CategoryBrandRelationEntity>().eq("catelog_id", catId));

        List<BrandVO> brandVOList = categoryBrandRelationEntityList.stream().map(categoryBrandRelationEntity -> {
            BrandVO brandVO = new BrandVO();
            brandVO.setBrandId(categoryBrandRelationEntity.getBrandId());
            brandVO.setBrandName(categoryBrandRelationEntity.getBrandName());
            return brandVO;
        }).collect(Collectors.toList());

        return brandVOList;
    }

}